<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/3/29
 * Time: 下午4:06
 */

namespace App\Http\Controllers;


use App\Server\MemberModel;

class BaseController extends Controller
{

    /**
     * token相关信息
     * @var null
     */
    public $token = null;

    /**
     * 判断是否验证token
     * @var bool
     */
    private $is_token = false;

    /**
     * 是否是开发环境
     * @var bool
     */
    private $dev = false;

    /**
     * 用户ID
     * @var int
     */
    public $userId = 0;


    public function __construct()
    {
        header("Access-Control-Allow-Origin: * ");
        header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS");
        header("Access-Control-Allow-Headers: X-Requested-With, Origin, X-Csrftoken, Content-Type, Accept,Acc-Token");
        $this->is_token();
    }

    /**
     * 检测头部协议
     * Acc-Key为免过滤,默认为最高管理员权限的账号
     * Acc-Key : 57041f8f7dff9b67e3f97d7facbaf8d3
     * Acc-Token为登陆后获取的令牌信息,该信息获取数据在memcache
     */
    public function is_token()
    {
        if ($this->is_token === false) {
            return;
        }
        $head = request()->header();
        if (isset($head['acc-token']) && $this->dev) {
            if ($head['acc-token'] == '57041f8f7dff9b67e3f97d7facbaf8d3') {
            }
        } elseif (isset($head['acc-token'])) {
            $this->token = $head['acc-token'];
            if (!$this->token) {
                return $this->error('token不能为空', 208);
            }
            $memberDetail = MemberModel::where(['token' => $this->token])->first();
            if (!$memberDetail) {
                $this->userId = $memberDetail['id'];
                return $this->error('token有误,请重新登录', 208);
            }
            $this->userId = $memberDetail['id'];
        } else {
            $this->error('请登录', 10001);
        }
    }


    /**
     * @param bool $status
     */
    public function setToken($status = false)
    {
        $this->is_token = $status;
    }


    /**
     * 成功后返回数据
     *
     * @param null $data
     * @param int $code
     * @param string $msg
     * @return array
     */
    protected function success($data = null, $code = 200, $msg = 'ok')
    {
        return $this->result((int)$code, $msg, $data);
    }

    /**
     * 失败后返回数据
     * @param string $msg 失败后返回msg
     * @param int $code 默认403
     * @param array $data null
     * @return array
     */
    protected function error($msg, $code = 206, $data = [])
    {
        if (empty($data)) {
            $data = json_decode('{}');
        }
        return $this->result((int)$code, $msg, $data);
    }

    /**
     * 返回约定数据
     *
     * @param int $code 状态码
     * @param string $msg 返回消息
     * @param null $data 返回数据
     * @return array
     */
    private function result($code = 200, $msg = '', $data = null)
    {
        $resultJson = ['code' => $code, 'msg' => $msg, 'data' => $data];
        exit(response()->json($resultJson)->send());
    }

    public function p($data = array(), $type = null)
    {
        header('Content-Type:text/html;charset=utf-8');
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        !$type ? exit () : null;
    }

}